/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package userinterface;

import costoptiontree.CostOption;
import costoptiontree.CostOptionNode;
import costoptiontree.SingleOptionSelection;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;

/**
 *
 * @author Michael
 */
public class RootNodePanel extends javax.swing.JPanel implements ActionListener{
    private CostOptionNode node;
    private CostOptionNode clickedNode;
    private JDesktopPane desktop;
    private ArrayList<ChildListItem> childList = new ArrayList<>();
    
    
    private JButton dButton_costChanged = new JButton();
    public static final String ACTION_COST_CHANGED = "rootNodeCostChanged";
    /**
     * Creates new form RootNodePanel
     */
    public RootNodePanel() {
        initComponents();
        dButton_costChanged.setActionCommand(ACTION_COST_CHANGED);
        
    }
    public void addCostChangedListener(ActionListener listener){
        dButton_costChanged.addActionListener(listener);
    }
    
    public void displayCost(){
        jLabel_cost.setText(((Integer)node.cost()).toString());
        for (ChildListItem item : childList){
            item.refresh();
        }
    }
    
    private void addChild(CostOptionNode node){
        ChildListItem item = new ChildListItem(node);
        jPanel_childListPanel.add(item);
        childList.add(item);
    }
    
    public void setNode(CostOptionNode node){
        this.node = node;
        jLabel_nodeName.setText(node.getName());
        jTextArea_description.setText(node.getDescription());
              
        for (CostOptionNode child : node.getChildren()){
            addChild(child);
        }
        displayCost();   
    }

    public void setDesktop(JDesktopPane desktop) {
        this.desktop = desktop;
    }
        
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel_nodeName = new javax.swing.JLabel();
        jLabel_cost = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel_childListPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea_description = new javax.swing.JTextArea();

        setBorder(javax.swing.BorderFactory.createBevelBorder(0));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jLabel3.setText("$");

        jLabel_nodeName.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jLabel_nodeName.setText("NodeName");

        jLabel_cost.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jLabel_cost.setText("####");
        jLabel_cost.setBorder(javax.swing.BorderFactory.createBevelBorder(1));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel_nodeName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel_cost, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_nodeName)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jLabel_cost)))
                .addContainerGap())
        );

        jPanel_childListPanel.setLayout(new javax.swing.BoxLayout(jPanel_childListPanel, javax.swing.BoxLayout.Y_AXIS));
        jScrollPane2.setViewportView(jPanel_childListPanel);

        jTextArea_description.setColumns(20);
        jTextArea_description.setLineWrap(true);
        jTextArea_description.setRows(5);
        jTextArea_description.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jTextArea_description);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void actionPerformed(ActionEvent evt) {
        String command = evt.getActionCommand();
        System.out.println("\nAction in RootNodePanel "+node.getName()+": "+command);
        
        if (CostOptionNodeInternalFrame.ACTION_COST_CHANGED.equals(command)){
//            System.out.println("Action in CostOptionNodeInternalFrame: "+command);
            displayCost();
            dButton_costChanged.doClick();
            
        }
        
        else if (CostOptionNodeInternalFrame.ACTION_SELECTION_CHANGED.equals(command)){
            displayCost();
            dButton_costChanged.doClick();
        }
        
    }    
    
    private void nodeClicked(CostOptionNode node) {                                         
                      
            CostOptionNodeInternalFrame frame = new CostOptionNodeInternalFrame();
            frame.setNode(node);

            frame.addCostChangedListener(this);
            frame.addSelectionChangedListener(this);
            frame.setVisible(true);   

            if (desktop!=null){
                int nFrames = desktop.getAllFrames().length;
                desktop.add(frame);
                
                frame.setLocation(100*nFrames, 0);
            }
            else{
                getParent().add(frame);
                frame.setLocation(getLocation().x+100, getLocation().y + 50);
            }
            
            frame.moveToFront();
            try {
                frame.setSelected(true);
            } catch (PropertyVetoException ex) {
                Logger.getLogger(CostOptionNodeInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel_cost;
    private javax.swing.JLabel jLabel_nodeName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel_childListPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea_description;
    // End of variables declaration//GEN-END:variables

    private class ChildListItem extends JLabel{
        public CostOptionNode childNode;
        public ChildListItem(CostOptionNode child){
            super();
            childNode = child;
            refresh();
            addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if (evt.getClickCount()==2){
                    System.out.println("Mouse double-clicked on node "+ childNode.getName());
                    nodeClicked(childNode);
                }
            }
        });
        }
        public void refresh(){
            setText(childNode.getName()+", $"+childNode.cost());
        }
    }
}
